import {ref,refs} from "vue";

// function broadcast(componentName, eventName, params) {
// 	console.log(componentName, eventName, params)
// 	this.$children.forEach(child => {
// 		const name = child.$options.name;
// 		console.log(name)
//
// 		if (name === componentName) {
// 			child.$emit.apply(child, [eventName].concat(params));
// 		} else {
// 			broadcast.apply(child, [componentName, eventName].concat([params]));
// 		}
// 	});
// }

const broadcast1 = (componentName, eventName, params,context) => {
	console.log(componentName, eventName, params,context)
	console.log(context,'1')
	context.$children.forEach(child => {
		const name = child.$options.name;
		console.log(name)

		if (name === componentName) {
			child.$emit.apply(child, [eventName].concat(params));
		} else {
			broadcast.apply(child, [componentName, eventName].concat([params]));
		}
	});
}

const dispatch = (componentName, eventName, params) => {
	let parent = this.$parent || this.$root
	let name = parent.$options.name;
	while (parent && (!name || name !== componentName)) {
		parent = parent.$parent;
		if (parent) {
			name = parent.$options.name;
		}
	}
	if (parent) {
		parent.$emit.apply(parent, [eventName].concat(params))
	}
}

const broadcast = (componentName, eventName, params,context) => {
	broadcast1.call(this, componentName, eventName, params,context);
}


export {dispatch, broadcast}



